package com.lf.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lf.api.weixin.CoreService;
import com.lf.api.weixin.WeiXinUtil;

@Controller
public class WeiXinAction {
	// 与接口配置信息中的Token要一致
	private static Logger log = LoggerFactory.getLogger(WeiXinAction.class);

	
	/** 
     * 微信公众号: 验证服务器地址的有效性<br> 
     * 微信URL: http://IP:PORT/项目名称/wechatController/wechat.do<br> 
     * 开发者通过检验signature对请求进行校验。若确认此次GET请求来自微信服务器，请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败。<br> 
     * 参考api: http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 
     * @param request 
     * @param response 
     * @param signature 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 
     * @param timestamp 时间戳 
     * @param nonce 随机数 
     * @param echostr 随机字符串 
     * @date 2016年7月9日 上午11:10:30 
     */  
    @RequestMapping(value="/weixin/access", method = RequestMethod.GET)  
    public void getMethod(HttpServletRequest request,HttpServletResponse response,String signature,String timestamp,String nonce,String echostr) {  
        String token = "xiaoxiaoshutong";//配置里面的token  
        // 通过检验signature对请求进行校验，若校验成功则原样返回nonce，表示接入成功，否则接入失败
        PrintWriter out =null;  
    	try{
    		out = response.getWriter();
    		log.info("Weixin server is checking! param[signature=" + signature + ",timestamp=" + timestamp + ",nonce" + nonce + "]");
            if (WeiXinUtil.checkAccessSignature(signature, token, timestamp, nonce)) {
                out.print(echostr);
                log.info("Weixin check is ok! echostr=" + echostr);
            }else{
            	log.info("Weixin check is error!");
            	return;
            }
    	} catch (IOException e) {
			e.printStackTrace();
		}finally{
            out.close();
            out = null;
        }
    }  
	/**
	 * 	/**
	 * 验证消息合法性
	 * 
	 * @param String
	 *            signature 微信加密签名
	 * @param String
	 *            echostr 随机字符串
	 * @return String timestamp 时间戳
	 * @throws NoSuchAlgorithmException 
	 * @throws IOException 
	 * @throws String
	 *             nonce 随机数
	 * Date:2016年10月5日
	 * @author 小小书童
	 * @return void
	 */
	@RequestMapping(value="/weixin/access", method=RequestMethod.POST)
    public void postMethod(String signature, String timestamp, String nonce,String echostr,
    		HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		// 响应消息
		try {
			// 调用核心业务类接收消息、处理消息
			String respXml = CoreService.processRequest(request);
			out.print(respXml);
		} finally {
			out.close();
			out = null;
		}
	}
}